﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using ChinaSoft.BLL;
using ChinaSoft.Model;

namespace Admin.Sys.Area
{
    public partial class Area_Edit : System.Web.UI.Page
    {
        private BLL_T_SYS_AREA bllArea = new BLL_T_SYS_AREA();
        private Model_T_SYS_AREA modelArea = new Model_T_SYS_AREA();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
                ControlBtn(false, false, false, false, false);
                ControlTextBox(true, true, true);
            }
        }
        /// <summary>
        /// 控制控件的状态（禁用、启用）
        /// </summary>
        /// <param name="btnAdd"></param>
        /// <param name="btnDel"></param>
        /// <param name="btnUpdate"></param>
        /// <param name="btnSave"></param>
        /// <param name="btnCancel"></param>
        private void ControlBtn(bool btnAdd, bool btnDel, bool btnUpdate, bool btnSave, bool btnCancel)
        {
            imgBtnAdd.Enabled = btnAdd;
            imgBtnDel.Enabled = btnDel;
            imgBtnUpdate.Enabled = btnUpdate;
            imgBtnSave.Enabled = btnSave;
            imgBtnCancel.Enabled = btnCancel;
        }
        /// <summary>
        /// 绑定区域
        /// </summary>
        private void BindTreeView()
        {
            TreeView1.Nodes.Clear();
            ChinaSoft.BLL.Common.AreaTreeView bllAreaTreeView = new ChinaSoft.BLL.Common.AreaTreeView();
            bllAreaTreeView.BindArea(TreeView1);
        }
        /// <summary>
        /// 当树形菜单选择改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeViewChanged(TreeView1.SelectedNode.Value, TreeView1.SelectedNode.Text, TreeView1.SelectedNode.Depth);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, false, true);
            HiddenAreaId.Value = "";
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnDel_Click(object sender, ImageClickEventArgs e)
        {
            string areaID = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            bool result = bllArea.Delete(areaID);
            if (result)
            {
                ChinaSoft.Common.MessageBox.Show(this, "删除成功！");
                ControlTextBox(false, false, true);
                BindTreeView();
                HiddenAreaId.Value = "";
            }
            else
            {
                ChinaSoft.Common.MessageBox.Show(this, "该数据已经不存在");
                BindTreeView();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, false, false);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (HiddenAreaId.Value == "")
            {
                #region 添加
                modelArea.CREATE_TIME = DateTime.Now;
                modelArea.CREATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelArea.CREATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                modelArea.NAME = txtName.Value.Trim();
                modelArea.UPDATE_TIME = DateTime.Now;
                modelArea.UPDATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelArea.UPDATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                modelArea.AREA_CODE = txtCode.Value.Trim();
                modelArea.CITY = HiddenCity.Value;
                modelArea.COUNTY = HiddenCounty.Value;
                modelArea.LEVEL_NO = TreeView1.SelectedNode.Depth+1;
                modelArea.PARENT_ID = HiddenFatherId.Value;
                modelArea.PROVINCE = HiddenProvince.Value;
                modelArea.STREET = HiddenStreet.Value;
                string strGUID=Guid.NewGuid().ToString();
                modelArea.SYS_AREA_ID = strGUID;
                modelArea.VILLAGE = HiddenVillage.Value;

                bllArea.Add(modelArea);

                modelArea = bllArea.GetModel(strGUID);
                if(HiddenProvince.Value.Trim()=="")
                    modelArea.PROVINCE=strGUID;
                else if(HiddenCity.Value.Trim()=="")
                    modelArea.CITY=strGUID;
                else if(HiddenCounty.Value.Trim()=="")
                    modelArea.COUNTY=strGUID;
                else if(HiddenStreet.Value.Trim()=="")
                    modelArea.STREET=strGUID;
                else if(HiddenVillage.Value.Trim()=="")
                    modelArea.VILLAGE=strGUID;
                if (string.IsNullOrEmpty(modelArea.AREA_CODE))
                    modelArea.AREA_CODE = "";
                if (string.IsNullOrEmpty(modelArea.CITY))
                    modelArea.CITY = "";
                if (string.IsNullOrEmpty(modelArea.COUNTY))
                    modelArea.COUNTY = "";
                if (string.IsNullOrEmpty(modelArea.CREATOR_ID))
                    modelArea.CREATOR_ID = "";
                if (string.IsNullOrEmpty(modelArea.CREATOR_NAME))
                    modelArea.CREATOR_NAME = "";
                if (string.IsNullOrEmpty(modelArea.PROVINCE))
                    modelArea.PROVINCE = "";
                if (string.IsNullOrEmpty(modelArea.STREET))
                    modelArea.STREET = "";
                if (string.IsNullOrEmpty(modelArea.UPDATOR_ID))
                    modelArea.UPDATOR_ID = "";
                if (string.IsNullOrEmpty(modelArea.UPDATOR_NAME))
                    modelArea.UPDATOR_NAME = "";
                if (string.IsNullOrEmpty(modelArea.VILLAGE))
                    modelArea.VILLAGE = "";
                bllArea.Update(modelArea);
                #endregion
                ChinaSoft.Common.MessageBox.Show(this, "添加成功！");
                BindTreeView();
                ControlTextBox(false, false, true);
            }
            else
            {
                modelArea = bllArea.GetModel(HiddenAreaId.Value);
                #region 更新
                if (string.IsNullOrEmpty(modelArea.CREATOR_ID))
                    modelArea.CREATOR_ID = "";
                if (string.IsNullOrEmpty(modelArea.CREATOR_NAME))
                    modelArea.CREATOR_NAME = "";
                modelArea.NAME = txtName.Value.Trim();
                modelArea.UPDATE_TIME = DateTime.Now;
                modelArea.UPDATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelArea.UPDATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                modelArea.AREA_CODE = txtCode.Value.Trim();
                modelArea.CITY = HiddenCity.Value;
                modelArea.COUNTY = HiddenCounty.Value;
                modelArea.PARENT_ID = HiddenFatherId.Value;
                modelArea.PROVINCE = HiddenProvince.Value;
                modelArea.STREET = HiddenStreet.Value;
                modelArea.VILLAGE = HiddenVillage.Value;
                
                bllArea.Update(modelArea);

                #endregion
                ChinaSoft.Common.MessageBox.Show(this, "更新成功！");
                BindTreeView();
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnCancel_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            ControlTextBox(true, true, true);
        }

        /// <summary>
        /// 控制文本框
        /// </summary>
        /// <param name="txtNameDisabled"></param>
        /// <param name="txtCodeDisabled"></param>
        /// <param name="boolClear"></param>
        private void ControlTextBox(bool txtNameDisabled, bool txtCodeDisabled, bool boolClear)
        {
            if (boolClear)
            {
                txtName.Value = "";
                txtCode.Value = "";
            }
            txtName.Disabled = txtNameDisabled;
            txtCode.Disabled = txtNameDisabled;
        }

        protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            TreeViewChanged(e.Node.Value, e.Node.Text, e.Node.Depth);
        }
        protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            TreeViewChanged(e.Node.Value, e.Node.Text, e.Node.Depth);
        }
        private void TreeViewChanged(string areaId,string areaName,int areaDepth)
        {
            ControlBtn(true, true, true, false, false);
            txtName.Value = areaName;
            modelArea = bllArea.GetModel(areaId);
            if (modelArea != null)
            {
                txtName.Value = modelArea.NAME;
                txtCode.Value = modelArea.AREA_CODE;
                HiddenFatherId.Value = modelArea.PARENT_ID;
            }
            switch (areaDepth)
            {
                case 0:
                    HiddenProvince.Value = areaId;
                    break;
                case 1:
                    HiddenCity.Value = areaId;
                    break;
                case 2:
                    HiddenCounty.Value = areaId;
                    break;
                case 3:
                    HiddenStreet.Value = areaId;
                    break;
                case 4:
                    HiddenVillage.Value = areaId;
                    break;
            }
            ControlTextBox(true, true, false);
            HiddenAreaId.Value = areaId;
        }
        protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
        {
            TreeViewChanged(e.Node.Value, e.Node.Text, e.Node.Depth);
        }
    }
}